package io.jee.caisan.weixin.handler;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import io.jee.caisan.weixin.config.ConfigProperties;
import io.jee.caisan.weixin.config.Constant;
import io.jee.caisan.weixin.domain.SysActivity;
import io.jee.caisan.weixin.service.ActivityService;
import io.jee.caisan.weixin.service.MemberService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MenuHandler extends AbstractHandler {
	
	@Resource
	private MemberService memberService;
	
	@Resource
	private ConfigProperties configProperties;
	
	@Resource
	private ActivityService activityService;
	
	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) {
		try {
			if(WxConsts.MenuButtonType.CLICK.equalsIgnoreCase(wxMessage.getEvent())) {
				if(Constant.WX_BUTTON_SIGN.equals(wxMessage.getEventKey())) {//微信菜单点击签到
					//验证登录
					WxMpXmlOutMessage out = memberService.wxMpVerifyBind(wxMessage);
					if(out != null) return out;
					//签到
					out = memberService.wxMpClickSgin(wxMessage);
					if(out != null) return out;
				}
				//查询活动
				WxMpXmlOutMessage out = activityService.auto(wxMessage, SysActivity.TYPE_0, wxMessage.getEventKey());
				if(out != null) return out;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return WxMpXmlOutMessage.TEXT().content("服务器内部异常，请稍后再试").fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
		}
	}

}
